package com.bjsxt.jdk.eventdriven;

import java.beans.PropertyChangeSupport;

/**
 * @author lichunlong lcl253137@alibaba-inc.com
 * @desc 事件驱动版本实现: 也是发布订阅的一种实现形式
 * @date 2023-01-18
 */
public class EventDemo {

    public static void main(String[] args) {
        Person person = new Person();

        // 属性变更的广播源
        PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(person);

        // 注册监听器, lambda表达式代替PropertyChangeListener匿名类的写法
        propertyChangeSupport.addPropertyChangeListener("name", event -> {
            Person personBean = (Person) event.getSource();
            System.out.printf("Person[%s]的name属性变更,老值: %s, 新值: %s", personBean, event.getOldValue(), event.getNewValue());
        });

        // 触发事件
        propertyChangeSupport.firePropertyChange("name", null, "Melo");
    }
}
